#!/bin/bash
# Script to fix git push issues with large binary files

echo "=== Fixing Git Push Issues ==="
echo "Current directory: $(pwd)"

# Check current git status
git status

# List large files in git history
echo "=== Large files in git history ==="
git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
sed -n 's/^blob //p' |
sort --numeric-sort --key=2 |
cut -c 1-12,41- |
awk '$2 > 104857600'  # Files larger than 100MB

# Check for specific large files
echo "=== Checking for .npy files ==="
find . -name "*.npy" -type f -exec ls -lh {} \;

# Remove large binary files from git tracking
echo "=== Removing large binary files from git tracking ==="
# Remove .npy files from tracking
git rm --cached output/*.npy 2>/dev/null || true
git rm --cached output/*/*.npy 2>/dev/null || true
git rm --cached output/*/*/*.npy 2>/dev/null || true

# Add .npy files to .gitignore if not already there
echo "=== Updating .gitignore ==="
if ! grep -q "\.npy$" .gitignore 2>/dev/null; then
    echo "# Large binary files" >> .gitignore
    echo "*.npy" >> .gitignore
    echo "output/*.npy" >> .gitignore
    echo "output/*/*.npy" >> .gitignore
    echo "Added .npy files to .gitignore"
fi

# Clean untracked files
echo "=== Cleaning untracked files ==="
git clean -fdx --dry-run
echo "If you want to clean untracked files, run: git clean -fdx"

# Create a new commit with the cleaned state
echo "=== Creating commit with cleaned state ==="
git add .gitignore
git add -A
git commit -m "fix: remove large binary files and update .gitignore

- Removed large .npy files from git tracking
- Added .npy files to .gitignore to prevent future issues
- Fixed git push quota issues

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>" || echo "No changes to commit"

# Alternative approach: If the above doesn't work, use git filter-branch
echo "=== Alternative: Using git filter-branch for history cleanup ==="
echo "If push still fails, run this more aggressive cleanup:"
echo "git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch output/*.npy' --prune-empty --tag-name-filter cat -- --all"

# Show current status
echo "=== Current status ==="
git status

# Try to push
echo "=== Attempting to push ==="
git push origin main